package com.qf.controller;

import com.qf.pojo.Cart;
import com.qf.pojo.Goods;
import com.qf.pojo.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/3 14:58
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @Autowired
    GoodsService goodsService;

    @RequestMapping("/getCart")
    public ModelAndView getCart(HttpSession session, ModelAndView view){
        User user = (User) session.getAttribute("user");
        if (user==null){
            view.setViewName("redirect:/login.jsp");
            return view;
        }
        List<Cart> carts = cartService.findByUid(user.getId());
        view.addObject("carts",carts);
        view.setViewName("forward:/cart.jsp");
        return view;
    }

    @RequestMapping("/addCart")
    public ModelAndView addCart(
            @RequestParam(value = "goodsId") int goodsId,
            @RequestParam(value = "number")int number,
            ModelAndView view,
            HttpSession session){

        User user = (User) session.getAttribute("user");
       if (user==null){
           view.setViewName("redirect:/login.jsp");
           return view;
       }
        Goods good = goodsService.findById(goodsId);
        Cart cart = cartService.checkCartByIdAndPid(user.getId(), goodsId);
        if (cart==null){
            cart = new Cart();
            cart.setId(user.getId());
            cart.setPid(goodsId);
            cart.setNum(number);
            cart.setMoney(good.getPrice());
            cartService.add(cart);
        }else {
            int num = cart.getNum() + number;
            cart.setNum(num);
            cart.setMoney(num*good.getPrice());
            cartService.update(cart);
        }
        view.setViewName("redirect:/cartSuccess.jsp");
        return view;
    }

    @GetMapping("/addCartAjax")
    public void addCartAjax( @RequestParam(value = "goodsId") int goodsId,
                             @RequestParam(value = "number")int number,
                             HttpSession session
                             ){
        User user = (User) session.getAttribute("user");
        if (number==0){
            cartService.deleteGood(user.getId(),goodsId);
        }else {
            Cart cart = cartService.checkCartByIdAndPid(user.getId(), goodsId);
            Goods good = goodsService.findById(goodsId);
            int num = cart.getNum()+number;
                cart.setNum(num);
                cart.setMoney(num*good.getPrice());
                cartService.update(cart);

        }
    }

    @GetMapping("/clearCartAjax")
    public void clearCartAjax(HttpSession session){
        User user = (User) session.getAttribute("user");
        cartService.clearCart(user.getId());
    }
}
